package com.sl.hx.shop.ui.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sl.hx.shop.R;
import com.sl.hx.shop.base.BaseActivity;
import com.sl.hx.shop.base.BasePresenter;
import com.sl.hx.shop.bean.CollectGoodsBean;
import com.sl.hx.shop.bean.ShopDetail;
import com.sl.hx.shop.bean.SpecialList;
import com.sl.hx.shop.http.ApiFactory;
import com.sl.hx.shop.http.ApiRetrofit;
import com.sl.hx.shop.http.HttpUtil;
import com.sl.hx.shop.http.ProgressSubscribe;
import com.sl.hx.shop.ui.home.adapter.SpecialAdapter;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;

/**
 * 专题页面
 */
public class SpecialActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.refresh)
    SwipeRefreshLayout mRefresh;

    private SpecialAdapter mSpecialAdapter;
    private List<SpecialList> specialLists = new ArrayList<>();
    private boolean IsRefresh=true;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_special;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        initRecycle();
        getSpecial();
        refresh();
    }

    private void refresh() {
        mRefresh.setColorSchemeColors(ContextCompat.getColor(mContext, R.color.main_color));
        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getSpecial();
            }
        });
    }

    private void initRecycle() {
        mSpecialAdapter = new SpecialAdapter(specialLists);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mSpecialAdapter);
        mSpecialAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                SpecialListClick(mSpecialAdapter.getItem(position));
            }
        });
    }


    @Override
    protected void initData() {

    }


    @OnClick({R.id.back, R.id.title_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.title_layout:
                break;
        }
    }

    private void getSpecial() {
        Observable observable = ApiFactory.getInstance().getSpecial(ApiRetrofit.getCacheControl());
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<List<SpecialList>>(mContext) {
            @Override
            protected void _onNext(List<SpecialList> specialLists) {
                ShowSpecialList(specialLists);
            }

            @Override
            protected void _onError(String message) {
                if (mRefresh.isRefreshing()) {
                    mRefresh.setRefreshing(false);
                }
            }

        }, true, this, ActivityEvent.DESTROY);
    }

    private void ShowSpecialList(List<SpecialList> specialLists) {
        mSpecialAdapter.setNewData(specialLists);
        if (mRefresh.isRefreshing()) {
            mRefresh.setRefreshing(false);
        }
    }

    private void SpecialListClick(SpecialList item) {
        if (item.getFavorites_id()>0) {
            //跳选品库页面
            Intent intent = new Intent(mContext, SlectStoreActivity.class);
            intent.putExtra("slectstore_favorites_id",item.getFavorites_id());
            intent.putExtra("slectStore_name",item.getName());
            mContext.startActivity(intent);
        }
    }

}
